package com.brotherly.framework.service;

import java.util.HashMap;
import java.util.LinkedList;
import java.util.List;
import java.util.Map;
import java.util.Map.Entry;

import android.content.Context;
import android.content.Intent;

import com.brotherly.framework.event.EventService;
import com.brotherly.framework.http.HttpService;
import com.brotherly.framework.log.LogService;
import com.brotherly.framework.storage.SimpleStorageService;
import com.brotherly.framework.timer.TimerService;
import com.brotherly.framework.transaction.TransactionService;
import com.brotherly.framework.transaction.TransactionServiceHost;
import com.brotherly.mpgp.remoteinterface.RemoteServerService;
import com.brotherly.util.InternetResourceCache;

public class ServiceManager implements IServiceResult
{
    public static final String EVENT_SERVICE = EventService.description;
    public static final String HTTP_SERVICE = HttpService.description;
    public static final String LOG_SERVICE = LogService.description;
    public static final String SIMPLESTORAGE_SERVICE = SimpleStorageService.description;
    public static final String TIMER_SERVICE = TimerService.description;
    public static final String TRANSACTION_SERVICE = TransactionService.description;
    public static final String REMOTESEVER_SERVICE = RemoteServerService.description;
    private static final ServiceManager ServiceManager = new ServiceManager();

    public static ServiceManager getInstance()
    {
        return ServiceManager;
    }

    private List<IServiceResult> listeners = new LinkedList<IServiceResult>();

    private Map<String, IService> services = new HashMap<String, IService>();

    public ServiceManager()
    {}

    public synchronized void addServiceResult(IServiceResult serviceResult)
    {
        if (listeners.contains(serviceResult))
            return;

        listeners.add(serviceResult);
    }

    public Object getService(String name)
    {
        Object iif = services.get(name);
        return iif;
    }

    @Override
    public void onServiceStart(ServiceHost serviceHost)
    {
        services.putAll(serviceHost.iServices);
        for (Entry<String, IService> service : services.entrySet())
        {
            service.getValue().init();
        }
        InternetResourceCache.getInstance().loadCache((SimpleStorageService) getService(SIMPLESTORAGE_SERVICE));
        for (IServiceResult result : listeners.toArray(new IServiceResult[listeners.size()]))
        {
            try
            {
                result.onServiceStart(serviceHost);
            }
            catch (Exception e)
            {
                e.printStackTrace();
            }
        }
    }

    public synchronized void removeServiceResultListener(IServiceResult serviceResult)
    {
        if (!listeners.contains(serviceResult))
            return;

        listeners.remove(serviceResult);
    }

    public void setServiceResultListener(IServiceResult serviceResult)
    {
        addServiceResult(serviceResult);
    }

    public void startService(Context context)
    {
        context.startService(new Intent(context, TransactionServiceHost.class));
    }

    public void stopService(Context context)
    {
        context.stopService(new Intent(context, TransactionServiceHost.class));
    }

}
